﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TomNet.Json;

namespace TomNet.App.Web.Service
{
	public class FkPool
	{
        #region Models.APPModel.PoolCoinDetails PoolCoinDetails(int currencyId) 币种信息。

        /// <summary>
        /// 币种信息。
        /// </summary>
        /// <param name="currencyId"></param>
        /// <returns></returns>
        public static Model.PoolCoinDetails PoolCoinDetails(int currencyId = 4)
        {
            Model.PoolCoinDetails result = new Model.PoolCoinDetails();

            try
            {
                string url = "http://47.110.252.85/api/app/poolcoindetails";

                var parameter = new
                {
                    CurrencyId = currencyId
                };

                string html = Post(url, parameter.ToJsonString());
                if (!string.IsNullOrWhiteSpace(html))
                {
					AspNetCore.Data.AjaxResult apiResult = JsonHelper.FromJson<AspNetCore.Data.AjaxResult>(html);
                    result = JsonHelper.FromJson<Model.PoolCoinDetails>(apiResult.Data.ToString());
                }
            }
            catch
            {
                //Logger.Error(ex);
            }

            return result;
        }

		#endregion

		#region string Post(string url, string data, string token) Post发送数据到接口。

		/// <summary>
		/// Post发送数据到接口。
		/// </summary>
		/// <param name="url"></param>
		/// <param name="data"></param>
		/// <param name="token"></param>
		/// <returns></returns>
		public static string Post(string url, string data)
		{
			var result = "";

			var appSetting = System.Configuration.ConfigurationManager.AppSettings;
			//string apiKey = appSetting.AllKeys.Contains("APIKey") ? appSetting["APIKey"] : string.Empty;
			string apiSecret = appSetting.AllKeys.Contains("APISecret") ? appSetting["APISecret"] : string.Empty;
			//if (string.IsNullOrWhiteSpace(apiSecret))
			//	throw new Exception("无有效的接口信息");

			try
			{
				byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
				System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

				//写数据
				request.Method = "POST";
				request.ContentLength = bytes.Length;
				request.ContentType = "application/json";
				//request.Headers.Add("APIKey", apiKey);
				//request.Headers.Add("APISecret", apiSecret);
				//request.Headers.Add("Token", token);

				System.IO.Stream reqstream = request.GetRequestStream();
				reqstream.Write(bytes, 0, bytes.Length);

				//读数据
				request.Timeout = 300000;
				request.Headers.Set("Pragma", "no-cache");

				System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
				//token = response.GetResponseHeader("token");
				System.IO.Stream streamReceive = response.GetResponseStream();

				System.IO.StreamReader streamReader = new System.IO.StreamReader(streamReceive, System.Text.Encoding.UTF8);

				result = streamReader.ReadToEnd();

				//关闭流
				reqstream.Close();
				streamReader.Close();
				streamReceive.Close();
				request.Abort();
				response.Close();

				return result;
			}
			catch (Exception ex)
			{
				return ex.Message;
			}
		}

		#endregion
	}
}
